<?php
/**
 * 名称：PublicAction
 * 描述：后台显示管理
 * 作者：lan 时间：2013-05-02
 * 版本号：V1.0.0
 * ========================历史版本=================================
 * 序号	 版本号 	日期			姓名	 	描述
 * 1 	V1.0.0 	2013-05-02 	lan  	后台显示管理
 * ========================历史版本=================================
 * 说明：请在有更改程序的时候修改以上注释
 */
class PublicAction extends Action {
	public function index() {
		redirect ( URL( 'admin/Index/index' ) );
	}

	/* 登录 */
	public function login() {
		if (! isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {
			$this->display ();
		} else {
			$this->display('login');
		}
	}

	// 用户退出
	public function logout() {
		model('User')->userLogout();
		$this->assign('jumpUrl',URL('admin/Public/login'));
		$this->success(L('exitSuccess'));
	}

	/**
	 * 登录检测
	 * Create Data <2013-05-02>
	 */
	public function checkLogin() {
		if (empty ( $_POST ['username'] )) {
			$this->error ( L ( 'accountError' ) );
		} elseif (empty ( $_POST ['password'] )) {
			$this->error ( L ( 'passwordMust' ) );
		}
		// 生成认证条件
		$map = array ();
		// 支持使用绑定帐号登录
		$map['password'] = md5 ( $_POST ['password']);
		$map ['email|uname|phone'] = $_POST ['username'];
		$map ["is_init"] = array (
				'gt',
				0
		);
		$map ["is_active"] = array (
				'gt',
				0
		);

		import ( 'ORG.Util.RBAC' );
		$authInfo = RBAC::authenticate ( $map );
		if (time() - $_SESSION['build_image_verify_time'] > 180) {
			$_SESSION['verify'] = '';
		}
		if (!empty ( $_POST ['verify'] ) && session('verify') != md5 ( $_POST ['verify'] )) {
			$this->error ( L ( 'verifyError' ) );
		}
		// 使用用户名、密码和状态的方式进行认证
		if (false === $authInfo) {
			$this->error ( L ( 'accountDoesNotExist' ) );
		} else {
			if ($authInfo ['password'] != md5 ( $_POST ['password'] )) {
				$this->error ( L ( 'passwordError' ) );
			}
			$_SESSION [C ( 'USER_AUTH_KEY' )] = $authInfo ['uid'];
			$_SESSION ['loginUserName'] = $authInfo ['uname'];

			$adminList = model('User')->where ( "admin_level='1'" )->select ();

			foreach ( $adminList as $k => $v ) {
				if ($authInfo ['uid'] == $v ['uid']) {
					//记录session
					$_SESSION['mid']			= $v['uid'];
					$_SESSION['uname']			= $v['uname'];
					$_SESSION ['administrator'] = true;
					if(in_array($v['uid'],C('NOT_AUTH_ADMIN'))){
						$_SESSION[C('ADMIN_AUTH_KEY')]=$v['uid'];
					}
				}
			}
                        
			if($_POST['remember'] === "pow"){
				setcookie("ylsnsmid", $_SESSION['mid'], time()+7*24*3600,'/',C('COOKIE_DOMAIN'));
			}
			// 保存登录信息
			$mlog = M ( 'LoginRecord' );
			$data = array ();
			$data ['uid'] = $authInfo ['uid'];
			$data ['ctime'] = time ();
			$data ['login_count'] = array (
					'exp',
					'login_count+1'
			);
			$data ['ip'] = get_client_ip ();

			if ($mlog->find ( $authInfo ['uid'] )) {
				$mlog->save ( $data );
			} else {
				$mlog->add ( $data );
			}
			// 缓存访问权限
			RBAC::saveAccessList ();
                        
                                                
                        //多平台同时登录
                        $loginData['uid'] = $authInfo['uid'];
                        $loginData['username'] = $authInfo['uname'];
                        $loginData['email'] = $authInfo['email'];
                        $loginData['password'] = $authInfo['password'];
                        
                        common('Url')->getSysOperUrl('synlogin',$loginData);
                        
			$this->success ( L ( 'loginSuccess' ), URL( 'admin/Index/index' ) );
			echo '<script>parent.location.href="'.URL( 'admin/Index/index' ).'";</script>';
		}
	}

	/**
	 * 英文验证码
	 * Create Data <2013-05-02>
	 */
	public function image() {
		import ( "ORG.Util.Image" );
		$_SESSION['build_image_verify_time'] = time();
		Image::buildImageVerify ( 4 ); // 英文验证码
	}
}
